package com.atguigu.gmall.web.all.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.item.feign.ItemFeign;
import com.atguigu.gmall.list.feign.SearchFeign;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import com.atguigu.gmall.product.feign.ProductFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import com.atguigu.gmall.web.all.util.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

/**
 * 商品详情的controller
 * @author 搭忆长安
 * @version 1.0
 * @date 2021年07月01日 20:53
 * @ClassName:ItemController
 * @Description: TODO
 */
@Controller
public class ItemController {

    @Autowired
    private ItemFeign itemFeign;
    @Autowired
    private ProductFeign productFeign;

    @Autowired
    private SearchFeign searchFeign;

    @GetMapping(value = "/item/{skuId}")
    public String item(@PathVariable(value = "skuId") Long skuId, Model model){
        //调用商品详情微服务获取商品的详情信息
        Result<Map> itemInfo = itemFeign.getItemInfo(skuId);
        Map skuInfo = itemInfo.getData();
        //设置商品的详情数据
//        //商品的基本信息和图片列表
//        model.addAttribute("skuInfo", skuInfo.get("skuInfo"));
//        //类别信息
//        model.addAttribute("categoryView", skuInfo.get("category"));
//        //价格信息
//        model.addAttribute("price", skuInfo.get("price"));
//        //销售属性信息
//        model.addAttribute("spuSaleAttrList", skuInfo.get("saltAttrBySpuAndSku"));
//        //用户商品切换的键值对
//        model.addAttribute("valuesSkuJson", JSONObject.toJSONString(skuInfo.get("skuSaleAttrListBySpuId")));
        model.addAttribute(skuInfo);
        return "item/index1.html";
    }

    /**
     * 首页展示
     * @param model
     * @return
     */
    @GetMapping(value = "/index")
    public  String  index(Model model){
         //查询首页的分类信息
        List<JSONObject> categoryInfoList = productFeign.getCategoryInfoList();
        //设置到model中
        model.addAttribute("list",categoryInfoList);
        return "index/index.html";
    }

    @GetMapping(value = "/list")
    public  String  list(SearchParam searchParam,
                         @RequestParam Map<String, String> searchData, Model model){
        //远程调用搜索
        SearchResponseVo search = searchFeign.search(searchParam, searchData);
        //设置页面的数据
        model.addAttribute("goodsList", search.getGoodsList());
        model.addAttribute("trademarkList", search.getTrademarkList());
        model.addAttribute("attrsList", search.getAttrsList());
        model.addAttribute("searchData", searchData);
        Long total = search.getTotal();
        Integer pageNo = search.getPageNo();
        Integer pageSize = search.getPageSize();
        Page<Object> page = new Page<>(total, pageNo, pageSize);
        model.addAttribute("page", page);

        //获取当前的url
        String url = getUrl(searchData);
        model.addAttribute("url", url);
        //返回页面
        return "list/list.html";
    }

    private String getUrl(Map<String, String> searchData) {
        String url = "/list";
        for (Map.Entry<String, String> entry : searchData.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            if (!key.equals("order")&&!key.equals("pageNo")){
                url +=key +"=" +value+"&";
            }
        }
        return url.substring(0,url.length()-1);
    }
}
